package org.zsk.cs.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;
import org.zsk.cs.common.AppUtils;
import org.zsk.cs.dao.IdentityAuthDao;

import javax.sql.DataSource;

/**
 * 应用配置类
 * @author zsk
 * @version 1.0
 * Create on 2019/11/14
 */
@Configuration
public class ApplicationConfig {

    private final DataSource dataSource;
    private final IdentityAuthDao identityAuthDao;

    @Autowired
    public ApplicationConfig(DataSource dataSource, IdentityAuthDao identityAuthDao) {
        this.dataSource = dataSource;
        this.identityAuthDao = identityAuthDao;
    }

    @Bean
    public PersistentTokenRepository persistentTokenRepository(){
        JdbcTokenRepositoryImpl tokenRepository = new JdbcTokenRepositoryImpl();
        tokenRepository.setDataSource(dataSource);
        return tokenRepository;
    }

    @Bean
    PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    AppUtils initAppUtils(){
        return new AppUtils(identityAuthDao);
    }

}
